﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.Serialization;

namespace Snake.Model
{

    
    // A test object that needs to be serialized.
    [Serializable]
    public class snake 
    {
        private List<Point> cordList;
        public enum Direction { up, down, right, left };
        private Direction dir;
        private Point tete;
        private int nbPommeMangees;
        private int score=0;
        private int size;
        private int nbMoves;
        private string NamePlayer;
        private bool envoyer=false;

        //private List<PictureBox> pictureList;

        //Constructeur de copie
        public snake(snake snake)
        {
            this.cordList = snake.cordList;
            this.dir = snake.dir;
            this.tete = snake.tete;
            this.nbPommeMangees = snake.nbPommeMangees;
            this.score = snake.score;
            this.size = snake.size;
            this.nbMoves = snake.nbMoves;
            this.NamePlayer = snake.nameplayer;
            this.envoyer = snake.envoyer;

        }

        public snake()
        {
            //this.pictureList = new List<PictureBox>();
            this.dir = Direction.right;
            this.cordList = new List<Point>();
            this.size = 5;
            this.nbPommeMangees = 0;
            this.nbMoves = 0;
            this.envoyer = false;

            for(int i=0; i<this.size; i++) {
                Point point = new Point(0+i*10, 100);
                this.cordList.Add(point);
            }

            this.tete = new Point(10*(this.size-1), 100);
        }

        public void moveSnake()
        {
            Point nouvelleTete = new Point(this.tete.X, this.tete.Y);
            if (this.dir == Direction.right)
            {
                nouvelleTete.X = nouvelleTete.X + 10;
                if (nouvelleTete.X >400)
                    nouvelleTete.X = 0;
            }
            else if(this.dir == Direction.left)
            {
                nouvelleTete.X = nouvelleTete.X - 10;
                if (nouvelleTete.X < 0)
                    nouvelleTete.X = 400-10;
            }
            else if (this.dir == Direction.up)
            {
                nouvelleTete.Y = nouvelleTete.Y - 10;
                if (nouvelleTete.Y < 0)
                    nouvelleTete.Y = 300 - 10;
            }
            else
            {
                nouvelleTete.Y = nouvelleTete.Y + 10;
                if (nouvelleTete.Y > 300)
                    nouvelleTete.Y = 0;
            }
            this.cordList.RemoveAt(0);
            this.cordList.Add(nouvelleTete);
            this.tete = nouvelleTete;
        }

        public Boolean mordu()
        {
            int count = 0;
            foreach (Point point in this.cordList)
            {
                if (count < this.cordList.Count - 1)
                {
                    if (point.X == this.tete.X && point.Y == this.tete.Y)
                    {
                        return true;
                    }
                    else
                    {
                        count++;
                    }
                }
            }  
            return false;
        }

        public void changedirect(Keys Keydata)
        {
            switch (Keydata)
            {
                case Keys.Right:
                    if (this.dir != Direction.right && this.dir != Direction.left)
                    {
                        this.dir = Direction.right;
                    }
                    break;
                case Keys.Left:
                    if (this.dir != Direction.left && this.dir != Direction.right)
                    {
                        this.dir = Direction.left;
                    }
                    break;
                case Keys.Up:
                    if (this.dir != Direction.up && this.dir != Direction.down)
                    {
                        this.dir = Direction.up;
                    }
                    break;
                case Keys.Down:
                    if (this.dir != Direction.down && this.dir != Direction.up)
                    {
                        this.dir = Direction.down;
                    }
                    break;
            }
        }

        public void Grown()
        {
            this.size++;
            Point newTete = new Point(this.tete.X, this.tete.Y);
            if (this.dir == Direction.right)
                newTete.X = this.tete.X + 10;
            else if (this.dir == Direction.left)
                newTete.X = this.tete.X - 10;
            else if (this.dir == Direction.up)
                newTete.Y = this.tete.Y - 10;
            else
                newTete.Y = this.tete.Y + 10;

            this.cordList.Add(newTete);
            this.tete = newTete;
        }

        public void increaseMoves()
        {
            this.nbMoves++;
        }

        public void decreaseMoves()
        {
            this.nbMoves--;
        }


        //GERTTERS SETTERS
    /*    public List<PictureBox> ReturnList
        {
            set { pictureList = value; }
            get { return pictureList; }
        }
        */

        public int getDir()
        {
            if (this.dir == Direction.right)
            {
                return 0;
            }
            else if (this.dir == Direction.left)
            {
                return 1;
            }
            else if (this.dir == Direction.up)
            {
                return 2;
            }
            else if (this.dir == Direction.down)
            {
                return 3;
            }

            return 4;
        }

        public int Score
        {
            get { return this.score; }
            set { this.score = value; }
        }


        public string nameplayer
        {
            get { return this.NamePlayer; }
            set { this.NamePlayer = value; }
        }

        public List<Point> Cords {
            get { return this.cordList; }
            set {this.cordList.AddRange(value); }
        }

        public int Size
        {
            get { return this.size; }
            set { this.size = value; }
        }

        public int Moves
        {
            get { return this.nbMoves; }
            set { this.nbMoves = value; }
        }

        public Point Tete
        {
            get { return this.tete; }
            set { this.tete = value; }
        }

        public bool Envoyer
        {
            get { return this.envoyer; }
            set { this.envoyer = value; }
        }
    }
}
